layui.use(['form','layer','jquery','layedit','laydate'],function(){
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		layedit = layui.layedit,
		laydate = layui.laydate,
		$ = layui.jquery;
    //校验登录状态
    if(isNotLogIn()){
        return;
    }

    var newsId = getUrlParms("newsId");
    layedit.set({
        uploadImage: {
        url: '/CivilAffairs/news/uploadNewsPic' //接口url
             ,type: 'post' //默认post
        }}
    );
    //创建一个编辑器
    var editIndex = layedit.build('news_content');;
    //默认的新闻内容
    var newsContent = "";
	//如果是编辑页面,从后台获取新闻的内容
	if(!isNull(newsId)){
        var index = layer.load(3);
		$.ajax({
			url:"/CivilAffairs/news/getNewById",
			type:"post",
			data:{"newsId":newsId,"isManager":true},
			dataType:"json",
			success:function(result){
                layer.close(index);
				if(checkResult(result)){
					var news = result.data;
					if(isNull(news)){
						layer.msg("未获取到要编辑的新闻内容,请重试",{icon:2});
						return;
					}
					var newInfo = news[0];
					$("#newsTitle").val(newInfo.news_title);
                    $("#newsType").val(newInfo.news_type);
                    // 重新渲染选择框
					//form.render("select","newsTypeFilter");
                    //失效的新闻展示为未生效
                    if("0"==newInfo.valid_tag){
                        var isValid = $("#isValid");
                        isValid.removeAttr("checked");
					}
					form.render();
                    newsContent = newInfo.news_content;
                    $("#news_content").val(newsContent);
                    //重新创建文本
                    editIndex = layedit.build('news_content');
                    //给提交按钮赋值新闻的itemid
                    $("#submitNews").attr("itemid",newInfo.news_id);
				}
			},
            error : function(result) {
                layer.close(index);
                console.log("获取新闻内容异常:"+result.status+","+result.statusMessage+",newsId:"+newsId);
                layer.msg("系统异常,请稍后重试",{icon:2});
            }
		});
	}

    //form提交时校验,用来将layedit中的内容同步到textarea中
    form.verify({
        content: function(value) {
            return layedit.sync(editIndex);
        }
    });

 	var addNewsArray = [],addNews;
 	//添加&修改新闻的提交
 	form.on("submit(addNews)",function(data){
        //校验登录状态
        if(isNotLogIn()){
            return false;
        }
		var newsId = $(this).attr("itemid");
		var url = "";
		var reqData = "";
        var managerId = getCookie("managerId");
		//是添加新闻
		if(isNull(newsId)){
            newsId = "";
            url = "/CivilAffairs/manager/addNews";
            reqData = {"newsInfo":JSON.stringify(data.field),"managerId":managerId};
		}else{
            url = "/CivilAffairs/manager/modifyNews";
            reqData = {"newsInfo":JSON.stringify(data.field),"newsId":newsId,"managerId":managerId};
		}
		//提交修改
        var index = layer.load(3);
        $.ajax({
            url:url,
            type:"post",
            data:reqData,
            dataType:"json",
            success:function(result){
                layer.close(index);
                if(checkResult(result)){
                   layer.msg(result.detail,{icon:6});
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                }
            },
            error : function(result) {
                layer.close(index);
                console.log("获取新闻内容异常:"+result.status+","+result.statusMessage+",newsId:"+newsId);
                layer.msg("系统异常,请稍后重试",{icon:2});
            }
        });
 		return false;
 	})
	
});
